/**
 * html5 history api to maintain history
 * @ history.pushState(state,title,url);
 * @ history.replaceState(state,title,url);
 * @ window.addEventListener('popstate', function(){});
 */

define(function(){

    //var routeStripper = /^[#\/]|\s+$/g;

    var spliter = '#';

    var _history = {};

    _history.getHash = function() {
        var match = window.location.href.match(/#(.*)$/);
        return match ? match[1] : '';
    };

    _history.setHash = function(location,fragment,replace) {
        if (replace) {
            var href = location.href.replace(/(javascript:|#).*$/, '');
            if (history.replaceState) {
                history.replaceState({}, document.title, href + '#' + fragment); // using history.replaceState instead of location.replace to work around chrom bug
            } else {
                location.replace(href + '#' + fragment);
            }
        } else {
            // Some browsers require that `hash` contains a leading #.
            location.hash = '#' + fragment;
        }
    };

    _history.push = function(title,fragment){
        if(history.pushState) {
            window.history.pushState({ title: title }, title, window.location.href.split(spliter)[0] + spliter + fragment);
        }
    };

    _history.replace = function(title,fragment){
        if(history.replaceState) {
            window.history.replaceState({title: title}, title, window.location.href.split(spliter)[0] + spliter + fragment);
        }
    };

    return _history;
});